using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;
using TiaoTaoHelper.Wtm.Attributes;
using HengTong.ViewModel.SupplyChain.SysPurchaseRequestBillEntryVms;
using HengTong.Model.Db.SupplyChain.Purchase;
using HengTong.Model.Db.SupplyChain.Material;
using System;


namespace HengTong.Areas.SupplyChain.Controllers
{
    [Area("SupplyChain")]
    [AuthorizeJwtWithCookie]
    [ActionDescription("采购申请明细")]
    [ApiController]
    [Route("api/SysPurchaseRequestBillEntry")]
	  public partial class SysPurchaseRequestBillEntryController : BaseApiController
    {
        [ActionDescription("Sys.Search")]
        [HttpPost("Search")]
		public IActionResult Search(SysPurchaseRequestBillEntrySearcher searcher)
        {
            if (ModelState.IsValid)
            {
                var vm = Wtm.CreateVM<SysPurchaseRequestBillEntryListVm>(passInit: true);
                vm.Searcher = searcher;
                return Ok(vm.GetJsonForApi());
            }
            else
            {
                return BadRequest(ModelState.GetErrorJson());
            }
        }

        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public SysPurchaseRequestBillEntryVm Get(string id)
        {
            var vm = Wtm.CreateVM<SysPurchaseRequestBillEntryVm>(id);
            return vm;
        }

        [ActionDescription("Sys.Create")]
        [HttpPost("Add")]
        [PreventDuplicateSubmit]
        public  async Task<IActionResult> Add(SysPurchaseRequestBillEntryVm vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }

        }

        [ActionDescription("Sys.Edit")]
        [PreventDuplicateSubmit]
        [HttpPut("Edit")]
        public  async Task<IActionResult> Edit(SysPurchaseRequestBillEntryVm vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }
        
		    [HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        [PreventDuplicateSubmit]
        public IActionResult BatchDelete(string[] ids)
        {
            var vm = Wtm.CreateVM<SysPurchaseRequestBillEntryBatchVm>();
            if (ids != null && ids.Any())
            {
                vm.Ids = ids;
            }
            else
            {
                return Ok();
            }
            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }


        [ActionDescription("Sys.Export")]
        [HttpPost("ExportExcel")]
        public IActionResult ExportExcel(SysPurchaseRequestBillEntrySearcher searcher)
        {
            var vm = Wtm.CreateVM<SysPurchaseRequestBillEntryListVm>();
            vm.Searcher = searcher;
            vm.SearcherMode = ListVMSearchModeEnum.Export;
            return vm.GetExportData();
        }

        [ActionDescription("Sys.CheckExport")]
        [HttpPost("ExportExcelByIds")]
        public IActionResult ExportExcelByIds(string[] ids)
        {
            var vm = Wtm.CreateVM<SysPurchaseRequestBillEntryListVm>();
            if (ids != null && ids.Any())
            {
                vm.Ids = new List<string>(ids);
                vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
            }
            return vm.GetExportData();
        }

        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<SysPurchaseRequestBillEntryImportVm>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }
            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }

        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(SysPurchaseRequestBillEntryImportVm vm)
        {
            if (vm!=null && (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData()))
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm?.EntityList?.Count ?? 0);
            }
        }
        

        [HttpGet("GetSysPurchaseRequestBills")]
        public ActionResult GetSysPurchaseRequestBills()
        {
            return Ok(DC.Set<SysPurchaseRequestBill>().GetSelectListItems(Wtm, x => x.BillCode));
        }

        [HttpGet("GetSysMaterials")]
        public ActionResult GetSysMaterials()
        {
            return Ok(DC.Set<SysMaterial>().GetSelectListItems(Wtm, x => x.Name));
        }

        [HttpGet("GetSysMaterialSkus")]
        public ActionResult GetSysMaterialSkus()
        {
            return Ok(DC.Set<SysMaterialSku>().GetSelectListItems(Wtm, x => x.Name));
        }

    }
}
